package org.lcx.taskvision.core.attribute;

import java.util.Date;
import java.util.EnumSet;

import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.lcx.taskvision.core.util.TaskVisionUtil;

/**
 * Provides a mapping from Mylyn task keys to ScrumVision keys.
 * 
 * @author Laurent Carbonnaux
 */
public class TaskVisionAttributeMapper extends TaskAttributeMapper {

	public enum Flag {
		READ_ONLY, HIDDEN, ATTRIBUTE, PEOPLE
	};

	public static final String NEW_CC = "task.common.newcc";

	public static final String REMOVE_CC = "task.common.removecc";

	public static final EnumSet<Flag> NO_FLAGS = EnumSet.noneOf(Flag.class);

	public static boolean isInternalAttribute(TaskAttribute attribute) {
		return TaskVisionAttribute.getBySvKey(attribute.getId()).isHidden();
	}

	public TaskVisionAttributeMapper(TaskRepository taskRepository) {
		super(taskRepository);
	}

	@Override
	public Date getDateValue(TaskAttribute attribute) {
		return TaskVisionUtil.parseDate(attribute.getValue());
	}

	@Override
	public String mapToRepositoryKey(TaskAttribute parent, String key) {
		TaskVisionAttribute attribute = TaskVisionAttribute.getByTaskKey(key);
		return (attribute != null) ? attribute.getSvKey() : key;
	}

	@Override
	public void setDateValue(TaskAttribute attribute, Date date) {
		if (date == null) {
			attribute.clearValues();
		} else {
			attribute.setValue(TaskVisionUtil.toDate(date));
		}
	}

}
